extends CenterContainer


onready var z_op = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/z/OptionButton
onready var name_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/name/LineEdit
onready var type_op = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/type/OptionButton
onready var res_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/res/LineEdit
onready var dialog_op = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/dialog/DialogSelect
onready var fight_dialog_op = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/fight_dialog/DialogSelect
onready var drama_op = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/drama/DramaSelect
onready var show_op = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/show/OptionButton
onready var npc_has_combat_op = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/npc_has_combat/OptionButton
onready var dir_op = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/dir/OptionButton
onready var move_type_op = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/move_type/OptionButton
onready var normal_exists_op = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/normal_exists/OptionButton
onready var x_offset_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/x_offset/SpinBox
onready var y_offset_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/y_offset/SpinBox
onready var day_time_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/day_time/SpinBox
onready var wake_up_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/wake_up/SpinBox
onready var detection_range_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/detection_range/SpinBox
onready var fixed_walk_move_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/fixed_walk_move/TextEdit

onready var start_show_time_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/StartShowTime/TextEdit
onready var end_show_time_input = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/EndShowTime/TextEdit

onready var position_ui = $PanelContainer/VBoxContainer/ScrollContainer/VBoxContainer/position/position


var edit_data = null



func set_edit_data(data):
	edit_data = data
		
	if edit_data:
		position_ui.text = str("(",data.x, " ", data.y, ")")
		z_op.select(z_op.get_item_index(data["layer"]))
		name_input.text = edit_data.get("name", "")
		res_input.text = edit_data.get("npcRes", "")
		type_op.select(type_op.get_item_index(edit_data["npcType"]))
		dialog_op.select_value(edit_data["npcDialogId"])
		fight_dialog_op.select_value(edit_data["npcFightDialogId"])
		drama_op.select_value(edit_data["npcDramaId"])
		show_op.select(show_op.get_item_index(edit_data["show"]))
		dir_op.select(dir_op.get_item_index(edit_data["dir"]))
		move_type_op.select(move_type_op.get_item_index(edit_data["moveType"]))
		npc_has_combat_op.select(npc_has_combat_op.get_item_index(edit_data["npcHasCombat"]))
		normal_exists_op.select(normal_exists_op.get_item_index(edit_data["normalExists"]))
		x_offset_input.value = edit_data["xOffset"]
		y_offset_input.value = edit_data["yOffset"]
		day_time_input.value = edit_data["dayTime"]
		wake_up_input.value = edit_data["wakeUp"]
		detection_range_input.value = edit_data["detectionRange"]
		fixed_walk_move_input.text = edit_data["fixedWalkMove"]
		if edit_data.has("startShowTime") and edit_data["startShowTime"] > 0:
			start_show_time_input.text = Time.get_datetime_string_from_unix_time(edit_data["startShowTime"] / 1000,true) 
		if edit_data.has("endShowTime") and edit_data["endShowTime"] > 0:
			end_show_time_input.text = Time.get_datetime_string_from_unix_time(edit_data["endShowTime"] / 1000,true) 
		
		
		$PanelContainer/VBoxContainer/modalTitle.text = "编辑Npc： %s" % name_input.text
	else:
		pass

func state_update():
	
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	# 更新当前坐标
	var position_info = map_view.get_select_position_info()
	position_ui.text = str("(",position_info.x, " ", position_info.y, ")")
	

func _on_ok_button_up():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	var position_info = map_view.get_select_position_info()
	
	if edit_data:
		read_form(edit_data)
		
#		if !edit_data["name"]: 
#			MainTool.toast_info("名称不能为空!")
#			return
		
		DataSource.update_data(DataSource.map_npc, edit_data)
		
		MainTool.toast_info("更新Npc%s成功!" % edit_data["name"])
	else:
		var data = {
			"x": position_info.x,
			"y": position_info.y,
			"mapId": position_info["map_register"]["id"]
		}
		
		read_form(data)
		
#		if !data["name"]: 
#			MainTool.toast_info("名称不能为空!")
#			return
		
		var pre_list = DataSource.get_data_by_list(DataSource.map_npc, position_info["map_register"]["id"], "mapId")
		for pre in pre_list:
			if pre["x"] == position_info.x and pre["y"] == position_info.y:
				MainTool.toast_info("该位置不可重复添加!")
				return
	
		DataSource.add_data(DataSource.map_npc, data)
		
		MainTool.toast_info("添加Npc成功!")
	
	map_view.entity_update()
	
	MainTool.getUILayer().pop_ui()

func read_form(dic):
	dic["npcRes"] = res_input.text
	dic["name"] = name_input.text
	dic["npcType"] = type_op.get_selected_id()
	dic["z"] = 1
	dic["layer"] = z_op.get_selected_id()
	dic["npcRes"] = res_input.text
	dic["npcType"] = type_op.get_selected_id()
	dic["npcDialogId"] = dialog_op.get_value()
	dic["npcFightDialogId"] = fight_dialog_op.get_value()
	dic["npcDramaId"] = drama_op.get_value()
	dic["show"] = show_op.get_selected_id()
	dic["dir"] = dir_op.get_selected_id()
	dic["moveType"] = move_type_op.get_selected_id()
	dic["npcHasCombat"] = npc_has_combat_op.get_selected_id()
	dic["normalExists"] = normal_exists_op.get_selected_id()
	dic["xOffset"] = x_offset_input.value
	dic["yOffset"] = y_offset_input.value
	dic["dayTime"] = day_time_input.value
	dic["wakeUp"] = wake_up_input.value
	dic["detectionRange"] = detection_range_input.value
	dic["fixedWalkMove"] = fixed_walk_move_input.text
	# npc 显示的时间和隐藏的时间，可以为空
	var temp_s = Time.get_unix_time_from_datetime_string(start_show_time_input.text)
	var temp_e = Time.get_unix_time_from_datetime_string(end_show_time_input.text)
	if temp_s > 0:
		dic["startShowTime"] = temp_s * 1000
	else:
		dic.erase("startShowTime")
	if temp_e > 0:
		dic["endShowTime"] = temp_e * 1000
	else:
		dic.erase("endShowTime") 

func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()
